package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusinesses(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusinesses(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<Business>(page.getTotal(), page.getRecords());

    }

    //根据id查询商机详细信息
    @Override
    public Business getBusinessById(Integer id) {

        return businessMapper.getBusinessById(id);

    }

    //跟进商机
    @Override
    public void trackBusiness(Business business) {
        // 更新线索状态为“跟进中”（假设状态值为3）
        business.setStatus(3);
        //更新updateTime
        business.setUpdateTime(LocalDateTime.now());
        this.updateById(business);
        //调用
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        //TODO
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser()); //获取当前登录用户id
        businessTrackRecord.setTrackStatus(business.getStatus());
        businessTrackRecord.setRecord(business.getRemark());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    //转客户处理
    @Override
    public void toCustomer(Integer id) {
        //1. 根据ID更新线索状态 (转为客户)
        //根据商机id查询商机信息
        Business business = businessMapper.selectById(id);
        //修改状态为“转客户”
        business.setStatus(5);
        //修改时间
        business.setUpdateTime(LocalDateTime.now());
        //《该步骤不太明白白》？？？？？？？？？？？？？？？
        businessMapper.updateById(business);//修改商机信息
        //2. 保存客户信息 (根据 商机 组装 商机信息)
        Customer customer = new Customer();
        //2.1拷贝商机索信息
        BeanUtils.copyProperties(business, customer);
        //2.2单独为其他字段赋值《具体添加那些字段不太清楚》
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);


    }
}
